<?php

namespace app\common\model;

use think\Model;

class Classes extends Model
{
    // 连接数据库名称
    //protected $connection = 'db2';
    // 表名
    protected $name = 'classes';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'created_at';
    protected $updateTime = false;

    const TYPE_XING_ZHENG = 1;
    const TYPE_ZOU_BAN = 2;
    const TYPE_GONG_GONG = 3;

    /**
     * @var array 班级类型 这个根教室类型一致
     */
    public static $typeList = [
        '1' => '行政班级',
        '2' => '走班班级',
        '3' => '公共班级',
    ];

    public function getLogoAttr($value)
    {
        if (strstr($value, '/uploads')) {
            $value = config('app_url') . $value;
        }

        return $value;
    }

    public function grade()
    {
        return $this->belongsTo(Grade::class);
    }

    public function school()
    {
        return $this->belongsTo('School');
    }

    public function garden()
    {
        return $this->belongsTo('Garden', 'garden_id', 'id', [], 'LEFT')->field('id,name');
    }

    public function building()
    {
        return $this->belongsTo('Building');
    }

    public function classroom()
    {
        return $this->belongsTo('Classroom');
    }

    public function student()
    {
        return $this->hasMany('Student', 'class_id', 'id');
    }

    public function getTypeTextAttr($value, $data)
    {
        return isset(self::$typeList[$data['type']]) ? self::$typeList[$data['type']] : '';
    }

}
